/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
 var maxSlidingWindow = function(nums, k) {
  let queue = []
  let res = []
  for(let i = 0;i < k;i++){
    while(queue.length && nums[queue[queue.length - 1]] <= nums[i]){
      queue.pop()
    }
    queue.push(i)
  }
  res.push(nums[queue[0]])
  for(let i = k;i < nums.length;i++){
    if(queue[0] <= i - k){
      queue.shift()
    }
    while(queue.length && nums[queue[queue.length - 1]] <= nums[i]){
      queue.pop()
    }
    queue.push(i)
    res.push(nums[queue[0]])
  }
  return res
};